package com.ruoyi.hospital.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.datasource.DynamicDataSourceContextHolder;
import com.ruoyi.hospital.domain.HPatient;
import com.ruoyi.hospital.domain.HPatientPdf;
import com.ruoyi.hospital.domain.ViewCpjsPatlist;
import com.ruoyi.hospital.mapper.HPatientMapper;
import com.ruoyi.hospital.service.IHpatientPdfService;
import com.ruoyi.hospital.service.IHpatientService;
import com.ruoyi.hospital.service.IViewCpjsPatlistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.*;

/**
 * 患者管理serviceimpl
 *
 * @author liz
 * @date 2023-10-18 10:37
 */
@Service
public class HPatientServiceImpl extends ServiceImpl<HPatientMapper, HPatient> implements IHpatientService {

    @Autowired
    HPatientMapper patientMapper;
    @Autowired
    private IViewCpjsPatlistService viewCpjsPatlistService;

    @Autowired
    private IHpatientPdfService patientPdfService;

    /**
     * 获取患者列表
     */
    @Override
    public List<HPatient> selectPatientList(HPatient patient) {
        List<HPatient> list = patientMapper.getlist(patient);
//        if (list.size() <= 0) {
//            QueryWrapper queryWrapper = new QueryWrapper<ViewCpjsPatlist>();
//            if (StrUtil.isNotBlank(patient.getName())) {
//                queryWrapper.like("name", patient.getName());
//            }
//            if (StrUtil.isNotBlank(patient.getInpatientNo())) {
//                queryWrapper.eq("inpatient_no", patient.getInpatientNo());
//            }
//            queryWrapper.ge("effective_e", LocalDate.now());
//            DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SLAVE.name());
//            List<ViewCpjsPatlist> vList = viewCpjsPatlistService.list(queryWrapper);
//            DynamicDataSourceContextHolder.clearDataSourceType();
//            vList.forEach(item -> {
//                HPatient hPatient = this.getOne(new QueryWrapper<HPatient>().eq("inpatient_no", item.getInpatientNo()));
//                if (ObjectUtil.isEmpty(hPatient)) {
//                    // 保存患者
//                    hPatient = new HPatient();
//                    BeanUtil.copyProperties(item, hPatient);
//                    hPatient.setCreateTime(new Date());
//                    hPatient.setUpdateTime(new Date());
//                    this.saveOrUpdate(hPatient);
//                }
//            });
//            list = patientMapper.getlist(patient);
//        }
        return list;
    }

    /**
     * 根据Id获取患者详情和报销详情
     */
    @Override
    public Object selectById(Integer id) {
        Map map = new HashMap();
        HPatient patient = patientMapper.selectInfoById(id);
        map.put("patient", patient);
        return map;
    }

    /**
     * 根据Id修改患者报销状态
     */
    @Override
    public void updateStatusById(Integer id, String settleType, String settleName) {
        this.update(new UpdateWrapper<HPatient>().set("settle_type", settleType).set("settle_name", settleName).eq("id", id));
    }
    /**
     * 根据住院号修改报销状态及pdf
     */
    @Override
    public void updateStatusByInpatientNo(Integer id, String settleType, String settleName, String pdfId) {
        patientMapper.updateStatusByInpatientNo(id,settleType,settleName,pdfId);
    }

    @Override
    public List<HPatient> selectPatientListAudit(HPatient patient) {
        List<HPatient> list = patientMapper.getlistAudit(patient);
        return list;
    }

    @Override
    public Integer selectHPatientAuditGain() {
        int count = 0;
        List<HPatient> list = patientMapper.selectHPatientAuditGain();
        for (HPatient hPatient : list) {
            DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SLAVE.name());
            ViewCpjsPatlist viewCpjsPatlist = viewCpjsPatlistService.getOne(new QueryWrapper<ViewCpjsPatlist>().ge("effective_e", LocalDate.now()).eq("inpatient_no", hPatient.getInpatientNo()));
            DynamicDataSourceContextHolder.clearDataSourceType();
            if (viewCpjsPatlist != null) {
                if (viewCpjsPatlist.getSettleType() != null) {
                        patientMapper.updateSettleType(hPatient.getInpatientNo(),Integer.parseInt(viewCpjsPatlist.getSettleType()), viewCpjsPatlist.getSettleName());
                    count++;
                }
            }
        }
        return count;
    }

    @Override
    public Map<String, Object> getInfo() {
        HashMap<String, Object> map = new HashMap<>();
       Integer countAll =  patientMapper.selectCountAll();
       Integer reimburseAll =  patientMapper.selectReimburseAll();
       Integer todayCount =  patientMapper.selectTodayCount();
       Integer reimburseCount =  patientMapper.selectReimburseCount();
        map.put("countAll",countAll);
        map.put("reimburseAll",reimburseAll);
        map.put("todayCount",todayCount);
        map.put("reimburseCount",reimburseCount);
        return map;
    }



    @Override
    public List<HPatient> reloadPieIndex() {
//        List<Map<String, Object>>  dayCount = patientMapper.select7DayCount();
//        List<Map<String, Object>> scale = patientMapper.selectScale();
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("dayCount",dayCount);
        List<HPatient> hPatients = patientMapper.select7DayCount();

        return hPatients;
    }
}
